package org.orr.budgetpro.services;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class SecurityService implements UserDetailsService {

	@Override
	public UserDetails loadUserByUsername(String userId)
			throws UsernameNotFoundException, DataAccessException {
		// Declare a new Spring User
		UserDetails user = null;

		if (userId == null || userId.trim().length() == 0)
			throw new UsernameNotFoundException("User ID is required");

		// For this demo application, all users have a password of "spider"
		user = new User(userId, "spider", true, true, true, true,
				getAuthorities(userId));

		return user;
	}

	private Collection<GrantedAuthority> getAuthorities(String userId) {
		Set<GrantedAuthority> auths = new HashSet<GrantedAuthority>();
		auths.add(new GrantedAuthorityImpl("ROLE_USER"));

		return auths;
	}
}
